<template>
  <div id="album">
    <div class="album-main">
      <div class="title">
        <HomeTitle title="相册管理" Eng="My Albums" />
        <!-- <el-breadcrumb style="margin-top: 10px">
          <el-breadcrumb-item v-for="(v, k) in album.menus" :key="k"
            ><span style="color: #fff">{{ v.label }}</span></el-breadcrumb-item
          >
        </el-breadcrumb> -->
      </div>

      <div class="content">
        <myLoading v-if="loading"></myLoading>
        <template v-else>
          <template v-if="album.mode == 'album'">
            <div class="btns">
              <el-button type="primary" @click="toAdd">新建相册</el-button>
            </div>
            <div class="list">
              <div
                class="item"
                v-for="(v, k) in album.list"
                :key="k"
                @mouseover="v.showBtn = true"
                @mouseout="v.showBtn = false"
                @click="toDetail(v)"
              >
                <div class="name">{{ v.GroupName }}</div>
                <div class="btns" v-show="v.showBtn">
                  <el-button type="success" @click.stop="auth(v)">
                    授权</el-button
                  >
                  <el-button type="primary" @click.stop="save(v)"
                    >修改</el-button
                  >
                  <el-button type="danger" @click.stop="del(v)">删除</el-button>
                </div>
              </div>
            </div>
          </template>
          <template v-else>
            <div style="display: flex; margin: 10px">
              <div
                style="
                  margin: 10px 10px 0 0;
                  cursor: pointer;
                  display: inline-block;
                "
                @click="album.mode = 'album'"
              >
                <el-icon>
                  <ArrowLeftBold></ArrowLeftBold>
                </el-icon>
                <span style="position: relative; top: -2px">返回</span>
              </div>
              <el-button
                type="primary"
                style="display: inline-block"
                @click="toUpload"
                >上传图片</el-button
              >
            </div>
            <div class="pic-list">
              <div
                class="item"
                @mouseover="v.showBtn = true"
                @mouseout="v.showBtn = false"
                v-for="(v, k) in album.pics"
                :key="k"
              >
                <div class="img">
                  <el-image
                    preview-teleported
                    style="width: 200px; height: 200px"
                    :preview-src-list="[baseApi + v.WatermarkFileUrl]"
                    :src="baseApi + v.WatermarkFileUrl"
                  ></el-image>
                  <div class="btns" v-show="v.showBtn">
                    <el-button type="primary" @click.stop="editPic(v)"
                      >編輯</el-button
                    >
                    <el-button type="danger" @click.stop="delPic(v)"
                      >刪除</el-button
                    >
                  </div>
                </div>
                <div class="tips">
                  <div class="name">{{ v.PictureName }}</div>
                  <div class="time">
                    {{ moment(v.UploadTime).format("YYYY-MM-DD HH:mm:ss") }}
                  </div>
                </div>
              </div>
            </div>
            <div class="footer">
              <el-pagination
                v-model:currentPage="picParams.PageIndex"
                :page-size="picParams.PageSize"
                :small="true"
                :background="true"
                layout="total, prev, pager, next"
                :total="album.count"
                @current-change="handleCurrentChange"
              />
            </div>
          </template>
        </template>
      </div>
    </div>
    <el-dialog
      v-model="album.showAlbum"
      :title="album.title"
      width="30%"
      draggable
    >
      <el-form :model="album.formData" label-width="100">
        <el-form-item label="相册名称" porp="GroupName">
          <el-input
            v-model="album.formData.GroupName"
            placeholder="相册名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="album.showAlbum = false">取消</el-button>
          <el-button :loading="album.loading" type="primary" @click="add()"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
    <el-dialog
      v-model="album.showPhoto"
      title="修改图片信息"
      width="30%"
      draggable
    >
      <el-form :model="album.formPhoto" label-width="100px">
        <el-form-item label="图片名称" porp="PictureName">
          <el-input
            v-model="album.formPhoto.PictureName"
            placeholder="图片名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="主题" porp="ThemeID">
          <!-- <el-input
            v-model="album.formPhoto.ThemeID"
            placeholder="主题"
          ></el-input> -->
          <el-select v-model="album.formPhoto.ThemeID">
            <el-option
              v-for="(v, k) in album.themes"
              :key="k"
              :label="v.ThemeName"
              :value="v.ThemeID"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自定义标签" porp="CustomizingTag">
          <!-- <el-input
            v-model="album.formPhoto.CustomizingTag"
            placeholder="自定义标签"
          ></el-input> -->
          <div class="tags">
            <template v-if="album.formPhoto.CustomizingTag.length > 0">
              <el-tag
                v-for="(s, j) in album.formPhoto.CustomizingTag.split(',')"
                :key="j"
                plain
                closable
                @close="delTag(v, s)"
                >{{ s }}</el-tag
              >
            </template>
            <el-button
              style="margin: 0 10px"
              size="small"
              circle
              type="primary"
              @click="addTag(v)"
            >
              <el-icon>
                <Plus />
              </el-icon>
            </el-button>
            <el-input
              v-model="tagInput"
              v-if="album.showInput"
              @blur="tagBlur($event, v)"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item label="图片描述" porp="Description">
          <el-input
            v-model="album.formPhoto.Description"
            type="textarea"
            placeholder="图片描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="album.showPhoto = false">取消</el-button>
          <el-button :loading="album.loading" type="primary" @click="savePic()"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="album.showAuth" title="相册授权" width="30%" draggable>
      <el-tree
        :data="album.DepartmentList"
        ref="treeRef"
        node-key="MenuID"
        @check="getChecked"
        :default-checked-keys="album.authList"
        :props="album.props"
        show-checkbox
      ></el-tree>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="album.showAuth = false">取消</el-button>
          <el-button :loading="album.loading" type="primary" @click="authSubmit"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { onMounted, onUnmounted, reactive, ref, toRefs,getCurrentInstance  } from "vue";
import { useRoute, useRouter } from "vue-router";
import HomeTitle from "@/components/title.vue";
import * as AlbumApi from "@/api/album";

import * as OfficeApi from "@/api/office";
import * as HomeApi from "@/api/home";
import { storeUser } from "@/store/user";
import { ElMessageBox, ElNotification } from "element-plus";
// unoirt
import moment from "moment";
import bus from "../../utils/bus";
export default {
  name: "",
  components: {
    HomeTitle,
  },
  setup(props, ctx) {
    const user = storeUser();
    const router = useRouter();
    const album = reactive({
      menus: [],
      title: "",
      list: [],
      mode: "album",
      showAlbum: false,
      formData: {
        GroupName: "",
      },
      type: "",
      loading: false,
      pics: [],
      count: 0,
      formPhoto: {},
      themes: [],
      showInput: false,
      showAuth: false,
      DepartmentList: [],
      props: {
        label: "DepartmentName",
        id: "DepartmentId",
      },
      authList: [],
      choseList: [],
      current: {},
    });
    const loading = ref(false);
    const toAdd = () => {
      album.title = "新建相册";
      album.formData = {
        GroupName: "",
      };
      album.type = "add";
      album.showAlbum = true;
    };

    const add = async (v) => {
      album.loading = true;
      if (album.type == "add") {
        let { data } = await AlbumApi.add({
          UserAccount: user.UserAccount,
          GroupName: album.formData.GroupName,
        });

        album.loading = false;
        if (data.Status != 0) return;
        ElNotification({
          type: "success",
          message: "创建成功！",
          title: "创建相册",
        });
      } else {
        let { data } = await AlbumApi.save({
          GroupID: album.formData.GroupID,
          UserAccount: user.UserAccount,
          GroupName: album.formData.GroupName,
        });

        album.loading = false;
        if (data.Status != 0) return;
        ElNotification({
          type: "success",
          message: "修改成功！",
          title: "修改相册",
        });
      }
      album.loading = false;
      album.showAlbum = false;
      init();
    };
    const init = async () => {
      let { data } = await AlbumApi.getList({
        UserAccount: user.UserAccount,
        PageSize: 999,
        PageIndex: 1,
      });
      album.list = data.UserData.list.map((v) => {
        return {
          ...v,
          showBtn: false,
        };
      });
    };
    const getTheme = async () => {
      let { data } = await HomeApi.getTheme();
      album.themes = data.UserData;
    };
    const getDepartments = async () => {
      let { data } = await OfficeApi.getList();
      album.DepartmentList = data.UserData;
    };
    const getChecked = (a, b) => {
      album.choseList = [
        ...b.checkedNodes.map((v) => v.DepartmentId),
        ...b.halfCheckedNodes.map((v) => v.DepartmentId),
      ];
    };

    const { proxy } = getCurrentInstance();
    const auth = async (v) => {
      album.current = v;
      album.showAuth = true;
      if (proxy.$refs.treeRef) {
        proxy.$refs.treeRef.setCheckedNodes([], false);
      }
    };
    const authSubmit = async () => {
      if (album.choseList.length == 0) {
        ElNotification({
          type: "error",
          title: "授权",
          message: "请选择需要授权的科室",
        });
        return;
      }
      let { data } = await AlbumApi.auth({
        CurrentUserAccount: user.UserAccount,
        AlbumID: album.current.GroupID,
        DepartmentIDs: album.choseList.join(","),
      });
      if(data.Status != 0 )return
      ElNotification({
        type:'success',
        title:"授权",
        message:"授权成功"
      })
      album.showAuth = false
    };
    onMounted(() => {
      init();
      getTheme();
      getDepartments();
    });
    const save = (v) => {
      album.formData = v;
      album.title = "编辑相册";
      album.type = "edit";
      album.showAlbum = true;
    };
    const del = (v) => {
      ElMessageBox.confirm("确认删除该相册？", "删除", {
        type: "warning",
      }).then(async () => {
        let { data } = await AlbumApi.del({
          GroupID: v.GroupID,
          UserAccount: user.UserAccount,
        });
        if (data.Status != 0) return;
        ElNotification({
          type: "success",
          title: "删除",
          message: "删除成功!",
        });
        init();
      });
    };
    const picParams = reactive({
      GroupID: "",
      PageSize: 10,
      PageIndex: 1,
      CurrentUserAccount:user.UserAccount
    });
    const getPics = async () => {
      let { data } = await AlbumApi.getPics(picParams);
      album.pics = data.UserData.PictureList.map((v) => {
        return {
          ...v,
          showBtn: false,
        };
      });
      album.count = data.UserData.PictureCount;
    };
    const toDetail = async (v) => {
      // 获取图片
      picParams.GroupID = v.GroupID;
      getPics();
      album.mode = "pic";
    };
    const handleCurrentChange = async (v) => {
      picParams.PageIndex = v;
      getPics();
    };
    const delPic = (v) => {
      ElMessageBox.confirm("確定刪除?", "刪除", {
        type: "warning",
      }).then(async () => {
        let { data } = await AlbumApi.delPic({
          PictureID: v.PictureID,
          UserAccount: user.UserAccount,
        });
        if (data.Status != 0) return;
        ElNotification({
          type: "success",
          title: "刪除",
          message: "刪除成功!",
        });
        getPics();
      });
    };
    const editPic = (v) => {
      album.formPhoto = v;
      album.showPhoto = true;
    };
    const savePic = async (v) => {
      let { data } = await AlbumApi.savePic({
        UserAccount: user.UserAccount,
        PictureID: album.formPhoto.PictureID,
        PictureName: album.formPhoto.PictureName,
        ThemeID: album.formPhoto.ThemeID,
        CustomizingTag: album.formPhoto.CustomizingTag,
        Description: album.formPhoto.Description,
      });
      if (data.Status != 0) return;
      album.showPhoto = false;
      ElNotification({
        type: "success",
        title: "修改",
        message: "修改成功!",
      });
      getPics();
    };
    const addTag = (v) => {
      tagInput.value = "";
      album.showInput = true;
    };
    const tagBlur = (e, v) => {
      if (!album.formPhoto.CustomizingTag) {
        album.formPhoto.CustomizingTag += tagInput.value;
      } else {
        album.formPhoto.CustomizingTag += "," + tagInput.value;
      }
    };
    const delTag = (v, s) => {
      let arr = album.formPhoto.CustomizingTag.split(",");
      let j = arr.findIndex((a) => a == s);
      arr.splice(j, 1);
      album.formPhoto.CustomizingTag = arr.join(",");
    };
    const tagInput = ref("");
    const toUpload = () => {
      bus.$emit("toUpload");
      router.push("/manage/upload?GroupID=" + picParams.GroupID);
    };
    return {
      authSubmit,
      getChecked,
      auth,
      album,
      toAdd,
      add,
      loading,
      save,
      del,
      moment,
      toDetail,
      picParams,
      handleCurrentChange,
      delPic,
      editPic,
      savePic,
      addTag,
      tagBlur,
      delTag,
      tagInput,
      toUpload,
    };
  },
};
</script>

<style scoped lang="scss">
::v-deep(.el-form-item__label) {
  color: #fff !important;
}
::v-deep(.el-tree) {
  background: #2f3042 !important;
  color: #ccc !important;
}
::v-deep(.el-tree-node__content:hover) {
  background: #414255 !important;
  color: #fff !important;
}

::v-deep(.el-textarea__inner) {
  background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-dialog) {
  background: #2f3042 !important;
  color: #fff !important;
}
::v-deep(.el-dialog__title) {
  color: #fff !important;
}

::v-deep(.el-input__wrapper) {
  background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-table) {
  background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-table tr) {
  background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-table__cell) {
  background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-input__inner) {
  // background: transparent !important;
  color: #fff !important;
}
::v-deep(.el-table__body-wrapper) {
  height: 100%;
}
::v-deep(.el-table__inner-wrapper) {
  height: 100%;
}
#album {
  position: relative;
  width: 100%;
  height: 100%;
  border-top-left-radius: 30px;
  overflow-y: auto;
  background: var(--box-bg-color);

  .album-main {
    width: 100%;
    padding: 30px 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    .title {
      margin-bottom: 20px;
    }
    .content {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .list {
        margin-top: 20px;

        // overflow-y: scroll;
        display: flex;
        flex-wrap: wrap;
        // justify-content: space-between;
        .item {
          width: 20%;
          text-align: center;
          margin-right: 20px;
          margin-bottom: 20px;
          height: 200px;
          background: #6e6f81;
          border-radius: 10px;
          position: relative;
          cursor: pointer;
          overflow: hidden;
          &:hover {
            background: #6266b6;
          }
          .name {
            position: absolute;
            bottom: 80px;
            left: 50%;
            transform: translateX(-50%);
          }
          .btns {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            display: flex;
            .el-button {
              flex: 1;
              margin: 0 !important;
            }
          }
        }
      }
      .pic-list {
        display: flex;
        flex-wrap: wrap;
        flex: 1;
        .item {
          margin: 0 20px 20px 0;
          text-align: center;
          position: relative;
          width: 18%;
          &:nth-child(5n) {
            margin-right: 0;
          }
          .img {
            position: relative;
            .btns {
              width: 100%;
              position: absolute;
              bottom: 0;
              left: 0;
              display: flex;
              .el-button {
                flex: 1;
                margin: 0;
              }
            }
          }
          .tips {
            font-size: 12px;
            .name {
              margin: 8px 0;
            }
          }
        }
      }
      .footer {
        margin-top: 20px;
        // text-align: right;
        align-self: flex-end;
      }
    }
  }
}
</style>
